Home:ALL Converter>java.io.IOException: Server returned HTTP response code: 400 for URL: https://vcip-aadhaarxml.hyperverge.co/api/v1/verifyAadhaar

java.io.IOException: Server returned HTTP response code: 400 for URL: https://vcip-aadhaarxml.hyperverge.co/api/v1/verifyAadhaar

Ask Time:2022-09-08T12:56:12         Author:Sachin Singh

Json Formatter

public class Demo {

public static void main(String[] args) {
    
// TODO Auto-generated method stub
    JSONObject postdata = new JSONObject();
    postdata.put("sessionId", sessionId);
    postdata.put("aadhaarNoUser", "123456454567");
    postdata.put("securityCode", securityCode);
    postdata.put("match", true);
    postdata.put("aadhaarNoClient","123456454567");
    postdata.put("dob", "01/01/1990");
    postdata.put("gender", "Male");
    postdata.put("phoneNo", "9050956789");
    System.out.println("sending data "+postdata.toString());
    String verifyurl = "https://vcip-aadhaarxml.hyperverge.co/api/v1/verifyAadhaar";
    URL obj;
    StringBuffer response = new StringBuffer();
    try {
        obj = new URL(verifyurl);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        //con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("appId", appId);
        con.setRequestProperty("appKey", appKey);
        con.addRequestProperty("transactionId", "sample_transaction_ID");
        con.setRequestProperty("Connection", "Keep-Alive");
        con.setDoInput(true); // Allow Inputs
        con.setDoOutput(true); // Allow Outputs
        con.setUseCaches(false); // Don't use a Cached Copy
        try(DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
            wr.writeBytes(postdata.toString());
            wr.flush();
            wr.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        int responseCode = con.getResponseCode();
        System.out.println("GET Response Code :: " + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            System.out.println("POST request STATUS OK");
            // print result
        } else {
            System.out.println("Post request STATUS NOT OK");
        }
        System.out.println(response.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    System.out.println(response.toString());
    
}

}

This is the exception I am getting I am getting exception at line mentioned below- GET Response Code :: 400 java.io.IOException: Server returned HTTP response code: 400 for URL: https://vcip-aadhaarxml.hyperverge.co/api/v1/verifyAadhaar at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263) at com.ils.wheebox.common.Demo.main(Demo.java:52) Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://vcip-aadhaarxml.hyperverge.co/api/v1/verifyAadhaar at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:347) at com.ils.wheebox.common.Demo.main(Demo.java:50)

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); This request is returning normal response with postman but not with the code

Author:Sachin Singh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73643915/java-io-ioexception-server-returned-http-response-code-400-for-url-https-vc
yy